{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot Actions\n",
    "\n",
    "Plots can be configured to run code or other cells when the user clicks on or types into them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abc = 0; // test variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def random = new Random()\n",
    "def p = new Plot(showLegend: true, useToolTip: false);\n",
    "p << new Line(x: [1, 2, 3], y: [2, 3, 4], width: 10, displayName: \"line 1\")\n",
    "         .onClick({info -> info.graphics.displayName = \"new name\"})\n",
    "\n",
    "\n",
    "p << new Line(x: [1, 2, 3], y: [5, 6, 7,], width: 10, displayName: \"line 2\")\n",
    "         .onClick({info -> info.graphics.y[1] = random.nextInt(10)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def p = new Plot(showLegend: true, useToolTip: false);\n",
    "\n",
    "p << new Line(x: [1, 2, 3], y: [2, 3, 4], width: 10, displayName: \"line 1\")\n",
    "         .onClick({\n",
    "            abc++\n",
    "            beakerx.runByTag(\"on_click_any_action\")\n",
    "          })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "on_click_any_action"
    ]
   },
   "outputs": [],
   "source": [
    "println abc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = new Plot(useToolTip: false);\n",
    "plot << new Points(x: (1..5), y: (1..5), size: 12.0, color: Color.orange, outlineColor: Color.black, displayName: \"orange\").onClick(\"run_tag\")\n",
    "plot << new Points(x: (1..5), y: (3..8), size: 12.0, color: Color.green, outlineColor: Color.black, displayName: \"green\").onClick(\"run_tag\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag"
    ]
   },
   "outputs": [],
   "source": [
    "def details = plot.details\n",
    "def item = details.graphics\n",
    "def index = details.index\n",
    "def key = details.key\n",
    "def tag = details.tag\n",
    "def action = details.actionType\n",
    "println (\"You clicked on \" + item.displayName + \" \" + item.class.simpleName + \" (element with coordinates [\" + item.x[index] + \",\" + item.y[index] + \"])\")\n",
    "println \"Key pressed = \" + key + \" Tag = \" + tag  + \" Action = \" + action"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "barsPlot = new Plot(useToolTip: false);\n",
    "barsPlot << new Bars(x: (1..5), y: [5, 2, 4, 3, 7], color: Color.green, outlineColor: Color.black, width: 0.3)\n",
    "        \n",
    "        //Also buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook\n",
    "        .onKey(KeyboardCodes.SPACE, {info -> info.graphics.y[info.index]++})\n",
    "\n",
    "        //Also buttons like KeyboardCodes.DOWN_ARROW is handled by jupyter notebook\n",
    "        .onKey(KeyboardCodes.CAPS_LOCK, {info -> info.graphics.y[info.index]--})\n",
    "        \n",
    "        //Tag events working\n",
    "        .onKey(\"T\", \"run_tag2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag2"
    ]
   },
   "outputs": [],
   "source": [
    "def details = barsPlot.details\n",
    "def item = details.graphics\n",
    "def index = details.index\n",
    "def key = details.key\n",
    "def tag = details.tag\n",
    "def action = details.actionType\n",
    "println (\"Key action on \" + item.class.simpleName + \" (element with coordinates [\" + item.x[index] + \",\" + item.y[index] + \"])\")\n",
    "println \"Key pressed = \" + key + \" Tag = \" + tag  + \" Action = \" + action"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "barsPlot = new Plot(useToolTip: false);\n",
    "barsPlot << new Bars(x: (1..5), y: [5, 2, 4, 3, 7], color: Color.green, outlineColor: Color.black, width: 0.3)\n",
    "        \n",
    "        //Buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook\n",
    "        .onKey(KeyboardCodes.SPACE, {\n",
    "            abc++\n",
    "            beakerx.runByTag(\"run_tag3\")\n",
    "        })\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run_tag3"
    ]
   },
   "outputs": [],
   "source": [
    "println abc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "beakerx_kernel_parameters": {},
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
